
myApp.controller('couponListController',function($scope,toaster,$serCoupon,$uibModal,$filter,$state,$log){
    var ctrl = this;
    ctrl.vm = {
        data:{},
        search:{
            id:'',
            code:'',
            name:'',
            status:'',
        },
        searchFn:function(){
            vm.init();
        },
        reset:function(){
            ctrl.vm.search.id='';
            ctrl.vm.search.code='';
            ctrl.vm.search.name='';
            ctrl.vm.search.status='';
            vm.init();
        },
        disable:function(id){
            $serCoupon.disable({
                id:id
            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        enable:function(id){
            $serCoupon.enable({
                id:id
            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        give:function(id){
            $serCoupon.give({
                id:id
            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    $scope.vm={
        page:{}
    };
    /*跳转页面*/
    $scope.pageChanged = function() {
        vm.init($scope.vm.page.current_page)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 20,
        changeFn:function(){
            vm.init(0)
        }
    };
    $scope.model = {
        open: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'addCoupon.html',
                controller: 'addCouponCtrl',
                windowClass: 'couponOpen',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        open1: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'upCoupon.html',
                controller: 'upCouponCtrl',
                windowClass: 'couponOpen',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        open2: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'giveCoupon.html',
                controller: 'giveCouponCtrl',
                windowClass: 'couponOpen',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };
    var vm ={
        init:function(page){
            $serCoupon.getList({
                page:page||1,
                size:$scope.placement.selected,
                id:ctrl.vm.search.id||'',
                code:ctrl.vm.search.code||'',
                name:ctrl.vm.search.name||'',
                status:ctrl.vm.search.status||'',
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                    $scope.vm.page = res.addtion_data;
                }else{
                    ctrl.vm.data ="";
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    vm.init();
    myApp.controller('addCouponCtrl', function($scope, $uibModalInstance, info,$serCoupon, toaster) {
        $scope.vm = {
            name:'',
            code:'',
            status:'',
            total_amount:'',
            discount_amount:'',
            // discount_amount:'',
            // discount_amount:'',

        }
        $scope.ok = function () {
            $serCoupon.addIt({
                name:$scope.vm.name,
                code:$scope.vm.code,
                status:$scope.vm.status,
                total_amount:$scope.vm.total_amount,
                discount_amount:$scope.vm.discount_amount
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });
    myApp.controller('upCouponCtrl', function($scope, $uibModalInstance, info,$serCoupon, toaster) {
        $scope.vm = {
            name:'',
            code:'',
            status:'',
            total_amount:'',
            discount_amount:'',
        }


        function init(){
            $scope.vm.name = info.name
            $scope.vm.total_amount = info.total_amount
            $scope.vm.status = info.status
            $scope.vm.discount_amount = info.discount_amount
            $scope.vm.code = info.code
        };
        init();
        $scope.ok = function () {
            $serCoupon.updateIt({
                id:info.id,
                name:$scope.vm.name,
                total_amount:$scope.vm.total_amount,
                status:$scope.vm.status,
                code:$scope.vm.code,
                discount_amount:$scope.vm.discount_amount
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });
    myApp.controller('giveCouponCtrl', function($scope, $uibModalInstance, info,$serCoupon, toaster) {
        $scope.vm = {
            coupon_id:'',
            u_id:'',
        }
        $scope.ok = function () {
            $serCoupon.give({
                u_id:$scope.vm.id,
                coupon_id:info.id,
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });
});

